package com.web.controller;

import com.jfinal.core.Controller;
import com.web.util.HttpRequest;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by reeveslee on 16/7/12.
 */
public class UploadImgController extends Controller {
    private static final Logger LOGGER = Logger.getLogger(UploadImgController.class);

    public void uploadImg() {
        String serverId = getPara("serverId");
        String access_token = getPara("access_token");
        String imgSrc = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + access_token + "&media_id=" + serverId;
        String saveDir = "/kuibu/static/res/wechatAuth/images/";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");


        String filepath = "/kuibu/static/res/wechatAuth/images/" + simpleDateFormat.format(new Date()) + "/";
        String filename = String.valueOf(new Date().getTime()) + ".jpg";

        HttpRequest.downloadImgByNet(imgSrc, filepath, filename);

        setAttr("result", true);
        setAttr("imgSrc", "http://139.129.18.232/wechatAuth/images/" + simpleDateFormat.format(new Date()) + "/" + filename);

        HttpServletResponse response = getResponse();
        response.setHeader("Access-Control-Allow-Origin", "*");

        renderJson();
    }
}
